<template>
  <div>
    <Modal v-model="show" title="添加用户" :mask-closable="false" :closable="false">
      <Form ref="modalForm" :model="data" :rules="ruls" :label-width="80">
        <FormItem label="手机号码" prop="phone">
          <Input v-model.trim="data.phone"></Input>
        </FormItem>
        <FormItem label="用户名" prop="name">
          <Input v-model.trim="data.name"></Input>
        </FormItem>
        <FormItem label="密码" prop="password">
          <Input type="password" v-model.trim="data.password"></Input>
        </FormItem>
        <FormItem label="重复密码" prop="rePassword">
          <Input type="password" v-model.trim="data.rePassword"></Input>
        </FormItem>

        <FormItem label="角色组" prop="roles">
          <CheckboxGroup v-model="data.roles">
            <Checkbox v-for="(item,index) in roles" :label="index" :key="item.id">{{item.name}}</Checkbox>
          </CheckboxGroup>

          <!-- <RadioGroup v-model="data.roles">
            <Radio  v-for="(item,index) in roles" :label="index" :key="item.id">
              <span>{{item.name}}</span>
            </Radio>
          </RadioGroup> -->
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="default" :disabled="cancelDisabled||loading" @click="cancel(false)">取消</Button>
        <Button type="primary" :loading="loading" @click="ok">确定</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import { post } from "@/libs/axios-cfg";
export default {
  data() {
    const validateConfirmPwd = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.data.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      show: true,
      loading: false,
      cancelDisabled:false,
      data: {
        name: "",
        password: "",
        rePassword: "",
        phone: "",
        roles: [],
      },
      ruls: {
        name: [
          { required: true, message: "用户名不能为空" }
          // {pattern:/^(\w){4,16}$/,message:'用户名应为[A-Za-z0-9_]组成的4-16位字符'}
        ],
        phone: [
          { required: true, message: "手机号码不能为空" },
          {pattern:/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,message:'手机号码格式不正确'}
        ],
        password: [
          { required: true, message: "请填写密码" },
          {
            pattern: /^(\w){5,18}$/,
            message: "密码应为[A-Za-z0-9_]组成的5-18位字符"
          }
        ],
        rePassword: [{ validator: validateConfirmPwd }],
        roles: [{ required: true, message: "请至少选择一个角色" }]
      }
    };
  },
  props: {
    roles: {
      type: Array,
      default: []
    }
  },
  methods: {
    /**
     * @description 关闭Modal
     * @param reload 是否重新加载数据
     */
    cancel(reload = false) {
      this.$emit("cancel", "add", reload);
    },
    /**
     * @description 确定按钮单击回调
     */
    ok() {
      this.$refs.modalForm.validate(valid => {
        if (valid) {
          let roles = [];
          this.data.roles.forEach(el => {
            roles.push(this.roles[el].id+"");
          });
          let data = JSON.parse(JSON.stringify(this.data));
          data.roles = roles;
          this.add(data);
        }
      });
    },
    /**
     * @description 添加用户数据请求
     */
     add(data) {
      this.loading = true;
      var url = "/user/addUser";
       this.post(url, data, {}).then(res => {
         this.loading = false;
        if (this.checkIsSsuccess(res)) {
          this.cancelDisabled = true;
          this.$Message.success({
            content: "新增成功！",
            duration: 3,
            closable: true,
            onClose: () => {
              this.$emit("success");
              this.cancel(false);
            }
          });
        } else {
          this.$Message.error({
            content: res["msg"],
            duration: 0,
            closable: true
          });
        }
      });
    
    }
  }
};
</script>

